package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exceptions.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {

    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;

    @Autowired
    private ApartmentLabelService apartmentLabelService;

    @Autowired
    private ApartmentFeeValueService apartmentFeeValueService;

    @Autowired
    private ApartmentFacilityService apartmentFacilityService;

    @Autowired
    private GraphInfoService graphInfoService;

    @Autowired
    private RoomInfoService roomInfoService;

    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private FacilityInfoMapper  facilityInfoMapper;
    @Autowired
    private FeeValueMapper feeValueMapper;


    @Override
    public void findPage(Page<ApartmentItemVo> page, ApartmentQueryVo queryVo) {
        apartmentInfoMapper.queryPage(page, queryVo);
    }


    //自定义保存和更新方法
    @Override
    public void customSaveOrUpdate(ApartmentSubmitVo apartmentSubmitVo) {

        Long id = apartmentSubmitVo.getId();
        //  判定是否是更新
        boolean isUpdate = apartmentSubmitVo.getId() != null && apartmentSubmitVo.getId() != 0;

        // 调用saveOrUpdate方法
        saveOrUpdate(apartmentSubmitVo);

        //  判断是更新删除关联中间数据
        if (isUpdate) {
            // 删除公寓和标签中间表数据
            LambdaQueryWrapper<ApartmentLabel> labelLambdaQueryWrapper = new LambdaQueryWrapper<>();
            labelLambdaQueryWrapper.eq(ApartmentLabel::getApartmentId, id);
            apartmentLabelService.remove(labelLambdaQueryWrapper);

            //删除公寓和杂费中间表数据
            LambdaQueryWrapper<ApartmentFeeValue> apartmentFeeValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
            apartmentFeeValueLambdaQueryWrapper.eq(ApartmentFeeValue::getApartmentId, id);
            apartmentFeeValueService.remove(apartmentFeeValueLambdaQueryWrapper);

            //删除公寓和配套中间表数据
            LambdaQueryWrapper<ApartmentFacility> facilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
            facilityLambdaQueryWrapper.eq(ApartmentFacility::getApartmentId, id);
            apartmentFacilityService.remove(facilityLambdaQueryWrapper);

            //删除图片博阿婆数据
            LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
            graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
            graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId, id);
            graphInfoService.remove(graphInfoLambdaQueryWrapper);
        }

        // 插入中间表数据
        // 公寓标签中间表数据
        List<Long> labelIds = apartmentSubmitVo.getLabelIds();
        if (!CollectionUtils.isEmpty(labelIds)) {
            List<ApartmentLabel> apartmentLabelList = new ArrayList<>(labelIds.size());
            for (Long labelId : labelIds) {
                ApartmentLabel label = ApartmentLabel.builder().apartmentId(id).labelId(labelId).build();
                apartmentLabelList.add(label);
            }
            apartmentLabelService.saveBatch(apartmentLabelList);
        }

        // 公寓杂费中间表
        List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
        if (CollectionUtils.isEmpty(feeValueIds)) {
            List<ApartmentFeeValue> apartmentFeeValueList = new ArrayList<>(feeValueIds.size());
            for (Long feeValueId : feeValueIds) {
                ApartmentFeeValue feeValue = ApartmentFeeValue.builder().apartmentId(id).feeValueId(feeValueId).build();
                apartmentFeeValueList.add(feeValue);
            }
            apartmentFeeValueService.saveBatch(apartmentFeeValueList);
        }

        //公寓配套中间表信息
        List<Long> facilityInfoIds = apartmentSubmitVo.getFacilityInfoIds();
        if (CollectionUtils.isEmpty(facilityInfoIds)) {
            List<ApartmentFacility> apartmentFacilityList = new ArrayList<>(facilityInfoIds.size());
            for (Long facilityInfoId : facilityInfoIds) {
                ApartmentFacility apartmentFacility = ApartmentFacility.builder().apartmentId(id).facilityId(facilityInfoId).build();
                apartmentFacilityList.add(apartmentFacility);
            }
            apartmentFacilityService.saveBatch(apartmentFacilityList);
        }

        //图片保存
        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
        if (CollectionUtils.isEmpty(graphVoList)) {
            List<GraphInfo> graphInfoList = new ArrayList<>(graphVoList.size());
            for (GraphVo graphVo : graphVoList) {
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setName(graphVo.getName());
                graphInfo.setUrl(graphVo.getUrl());
                graphInfo.setItemType(ItemType.APARTMENT);
                graphInfo.setItemId(id);
                graphInfoList.add(graphInfo);
            }
            graphInfoService.saveBatch(graphInfoList);
        }

    }



    //按id删除公寓
    @Override
    public void customRemoveById(Long id) {
        // 1. 查询公寓下是否有房间
        LambdaQueryWrapper<RoomInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(RoomInfo::getApartmentId, id);
        long count = roomInfoService.count(lambdaQueryWrapper);
        // 2. 有房间，不能删除,抛出异常
        if (count > 0) {
            //有房间就抛出异常
            throw new LeaseException(ResultCodeEnum.DELETE_ERROR);
        }

        // 3. 没有房间(删除公寓,删除中间表)
        //删除图片
        LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId, id);
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
        graphInfoService.remove(graphInfoLambdaQueryWrapper);

        //删除公寓配套
        LambdaQueryWrapper<ApartmentFacility> facilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
        facilityLambdaQueryWrapper.eq(ApartmentFacility::getApartmentId, id);
        apartmentFacilityService.remove(facilityLambdaQueryWrapper);

        //删除公寓标签
        LambdaQueryWrapper<ApartmentLabel> labelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        labelLambdaQueryWrapper.eq(ApartmentLabel::getApartmentId, id);
        apartmentLabelService.remove(labelLambdaQueryWrapper);

        //删除公寓杂费值
        LambdaQueryWrapper<ApartmentFeeValue> apartmentFeeValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apartmentFeeValueLambdaQueryWrapper.eq(ApartmentFeeValue::getApartmentId, id);
        apartmentFeeValueService.remove(apartmentFeeValueLambdaQueryWrapper);

        //删除公寓信息
        removeById(id);

    }


    // 查询公寓详情信息
    @Override
    public ApartmentDetailVo customDetail(Long id) {
        //  查看公寓信息
        ApartmentInfo apartmentInfo = this.getById(id);
        if (apartmentInfo == null){
            return null;
        }
        // 查询公寓图片
        List<GraphVo> graphInfoList = graphInfoMapper.queryVo(id,ItemType.APARTMENT);
        //查询公寓标签
        List<LabelInfo> labelInfoList = labelInfoMapper.queryInfo(id);
        //查询公寓配套
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.queryInfo(id);
        //查询公寓杂费
        List<FeeValueVo> feeValueVoList = feeValueMapper.queryVo(id);

        // 封装数据
        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        apartmentDetailVo.setGraphVoList(graphInfoList);
        apartmentDetailVo.setLabelInfoList(labelInfoList);
        apartmentDetailVo.setFacilityInfoList(facilityInfoList);
        apartmentDetailVo.setFeeValueVoList(feeValueVoList);

        BeanUtils.copyProperties(apartmentInfo,apartmentDetailVo);
        return apartmentDetailVo;
    }

}





